import { ElNotification, ElMessageBox } from 'element-plus'
import nProgress from 'nprogress'

// 消息提示
export const toast = (message, type = 'success', dangerouslyUseHTMLString = true) => {
  ElNotification({
    message,
    type,
    dangerouslyUseHTMLString,
    duration: 3000,
  })
}

// 弹出提示框
export const showModal = (content = '提示内容', type = 'warning', title = '') => {
  return ElMessageBox.confirm(content, title, {
    confirmButtonText: '确定',
    cancelButtonText: '取消',
    type,
  })
}

// 显示全屏loading
export const showFullLoading = () => {
  nProgress.start()
}
// /隐藏全屏loading
export const hideFullLoading = () => {
  nProgress.done()
}

//弹出输入框
export const showPrompt = (tip, value = '') => {
  return ElMessageBox.prompt(tip, '', {
    confirmButtonText: '确认',
    cancelButtonText: '取消',
    inputValue: value,
  })
}

// 上移
export const useArrayMoveUp = (arr, index) => {
  swapArray(arr, index, index - 1)
}
// 下移
export const useArrayMoveDown = (arr, index) => {
  swapArray(arr, index, index + 1)
}

const swapArray = (arr, index1, index2) => {
  arr[index1] = arr.splice(index2, 1, arr[index1])[0]
  return arr
}
// sku排列算法
export function cartesianProductOf() {
  return Array.prototype.reduce.call(
    arguments,
    function (a, b) {
      var ret = []
      a.forEach(function (a) {
        b.forEach(function (b) {
          ret.push(a.concat([b]))
        })
      })
      return ret
    },
    [[]],
  )
}
// 将query对象转成url参数
export function queryParams(query) {
  let q = []
  for (const key in query) {
    if (query[key]) {
      q.push(`${key}=${encodeURIComponent(query[key])}`)
    }
  }
  let r = q.join('&')
  r = r ? '?' + r : ''
  return r
}
